/* Copyright (c) 2011-2012 ISP RAS, Moscow. For more information please write to
 
      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ru.ispras.coffeemachine.instrument.android;

import org.apache.bcel.generic.*;

import ru.ispras.coffeemachine.instrument.CoffeeInstructionFactory;
import ru.ispras.coffeemachine.util.Options;

/**
 * Coffee Machine, instrument for dynamic analysis Java-programs
 * 
 * @author Mikhail Ermakov (mermakov@ispras.ru)
 * @version 1.2.0
 */
public class AndroidInstructionFactory extends CoffeeInstructionFactory {

	private static final long serialVersionUID = 1L;
	
	public AndroidInstructionFactory(ConstantPoolGen cp) {

		super(cp);
		invokePrintMethod = cp.addMethodref("android.util.Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I");
	}

	public AndroidInstructionFactory(ClassGen cg, ConstantPoolGen cpg) {

		super(cg, cpg);
		invokePrintMethod = cp.addMethodref("android.util.Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I");
	}

	@Override
	public InstructionList createPrintln(String msg) {

		InstructionList il = new InstructionList();
		il.append(new PUSH(cp, Options.tracePrefix));
		il.append(new PUSH(cp, msg));
		il.append(new INVOKESTATIC(invokePrintMethod));
		il.append(new POP());
		return il;
	}

	@Override
	public InstructionList createDaemonPrintln() {
		
		InstructionList il = new InstructionList();
		il.append(new PUSH(cp, Options.tracePrefix));
		il.append(new ALOAD(0));
		il.append(new INVOKESTATIC(invokePrintMethod));
		il.append(new POP()); // Remove integer result
		return il;
	}
}
